<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目编号" prop="projectNo">
        <el-input
          v-model="queryParams.projectNo"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目状态" prop="projectStatus">
        <el-input
          v-model="queryParams.projectStatus"
          placeholder="请输入项目状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目地址" prop="projectAddress">
        <el-input
          v-model="queryParams.projectAddress"
          placeholder="请输入项目地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结构量(t)" prop="dtructuralQuantity">
        <el-input
          v-model="queryParams.dtructuralQuantity"
          placeholder="请输入结构量(t)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已产构件数" prop="constructedNumber">
        <el-input
          v-model="queryParams.constructedNumber"
          placeholder="请输入已产构件数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已发货构件数" prop="shippedQuantity">
        <el-input
          v-model="queryParams.shippedQuantity"
          placeholder="请输入已发货构件数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="未排构件数" prop="unscheduledNumber">
        <el-input
          v-model="queryParams.unscheduledNumber"
          placeholder="请输入未排构件数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开工时间" prop="commencementTime">
        <el-date-picker clearable
          v-model="queryParams.commencementTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开工时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完工时间" prop="completionTime">
        <el-date-picker clearable
          v-model="queryParams.completionTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择完工时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社会统一信用代码" prop="unifiedSocialCreditCode">
        <el-input
          v-model="queryParams.unifiedSocialCreditCode"
          placeholder="请输入社会统一信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户联系人" prop="customerContact">
        <el-input
          v-model="queryParams.customerContact"
          placeholder="请输入客户联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户联系方式" prop="customerInformation">
        <el-input
          v-model="queryParams.customerInformation"
          placeholder="请输入客户联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理费" prop="managementExpense">
        <el-input
          v-model="queryParams.managementExpense"
          placeholder="请输入管理费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="personCharge">
        <el-input
          v-model="queryParams.personCharge"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:maintenance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:maintenance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:maintenance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:maintenance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintenanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="number" />
      <el-table-column label="项目编号" align="center" prop="projectNo" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="项目类型" align="center" prop="projectType" />
      <el-table-column label="项目状态" align="center" prop="projectStatus" />
      <el-table-column label="项目地址" align="center" prop="projectAddress" />
      <el-table-column label="结构量(t)" align="center" prop="dtructuralQuantity" />
      <el-table-column label="已产构件数" align="center" prop="constructedNumber" />
      <el-table-column label="已发货构件数" align="center" prop="shippedQuantity" />
      <el-table-column label="未排构件数" align="center" prop="unscheduledNumber" />
      <el-table-column label="开工时间" align="center" prop="commencementTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.commencementTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完工时间" align="center" prop="completionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="社会统一信用代码" align="center" prop="unifiedSocialCreditCode" />
      <el-table-column label="客户联系人" align="center" prop="customerContact" />
      <el-table-column label="客户联系方式" align="center" prop="customerInformation" />
      <el-table-column label="管理费" align="center" prop="managementExpense" />
      <el-table-column label="负责人" align="center" prop="personCharge" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:maintenance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:maintenance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目编号" prop="projectNo">
          <el-input v-model="form.projectNo" placeholder="请输入项目编号" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="合同编号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="项目状态" prop="projectStatus">
          <el-input v-model="form.projectStatus" placeholder="请输入项目状态" />
        </el-form-item>
        <el-form-item label="项目地址" prop="projectAddress">
          <el-input v-model="form.projectAddress" placeholder="请输入项目地址" />
        </el-form-item>
        <el-form-item label="结构量(t)" prop="dtructuralQuantity">
          <el-input v-model="form.dtructuralQuantity" placeholder="请输入结构量(t)" />
        </el-form-item>
        <el-form-item label="已产构件数" prop="constructedNumber">
          <el-input v-model="form.constructedNumber" placeholder="请输入已产构件数" />
        </el-form-item>
        <el-form-item label="已发货构件数" prop="shippedQuantity">
          <el-input v-model="form.shippedQuantity" placeholder="请输入已发货构件数" />
        </el-form-item>
        <el-form-item label="未排构件数" prop="unscheduledNumber">
          <el-input v-model="form.unscheduledNumber" placeholder="请输入未排构件数" />
        </el-form-item>
        <el-form-item label="开工时间" prop="commencementTime">
          <el-date-picker clearable
            v-model="form.commencementTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开工时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完工时间" prop="completionTime">
          <el-date-picker clearable
            v-model="form.completionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择完工时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="社会统一信用代码" prop="unifiedSocialCreditCode">
          <el-input v-model="form.unifiedSocialCreditCode" placeholder="请输入社会统一信用代码" />
        </el-form-item>
        <el-form-item label="客户联系人" prop="customerContact">
          <el-input v-model="form.customerContact" placeholder="请输入客户联系人" />
        </el-form-item>
        <el-form-item label="客户联系方式" prop="customerInformation">
          <el-input v-model="form.customerInformation" placeholder="请输入客户联系方式" />
        </el-form-item>
        <el-form-item label="管理费" prop="managementExpense">
          <el-input v-model="form.managementExpense" placeholder="请输入管理费" />
        </el-form-item>
        <el-form-item label="负责人" prop="personCharge">
          <el-input v-model="form.personCharge" placeholder="请输入负责人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaintenance, getMaintenance, delMaintenance, addMaintenance, updateMaintenance } from "@/api/project/maintenance";

export default {
  name: "Maintenance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目维护表格数据
      maintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectNo: null,
        projectName: null,
        contractNo: null,
        projectType: null,
        projectStatus: null,
        projectAddress: null,
        dtructuralQuantity: null,
        constructedNumber: null,
        shippedQuantity: null,
        unscheduledNumber: null,
        commencementTime: null,
        completionTime: null,
        customerName: null,
        unifiedSocialCreditCode: null,
        customerContact: null,
        customerInformation: null,
        managementExpense: null,
        personCharge: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectNo: [
          { required: true, message: "项目编号不能为空", trigger: "blur" }
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        contractNo: [
          { required: true, message: "合同编号不能为空", trigger: "blur" }
        ],
        projectStatus: [
          { required: true, message: "项目状态不能为空", trigger: "blur" }
        ],
        projectAddress: [
          { required: true, message: "项目地址不能为空", trigger: "blur" }
        ],
        dtructuralQuantity: [
          { required: true, message: "结构量(t)不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: "社会统一信用代码不能为空", trigger: "blur" }
        ],
        customerContact: [
          { required: true, message: "客户联系人不能为空", trigger: "blur" }
        ],
        customerInformation: [
          { required: true, message: "客户联系方式不能为空", trigger: "blur" }
        ],
        managementExpense: [
          { required: true, message: "管理费不能为空", trigger: "blur" }
        ],
        personCharge: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目维护列表 */
    getList() {
      this.loading = true;
      listMaintenance(this.queryParams).then(response => {
        this.maintenanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        number: null,
        projectNo: null,
        projectName: null,
        contractNo: null,
        projectType: null,
        projectStatus: null,
        projectAddress: null,
        dtructuralQuantity: null,
        constructedNumber: null,
        shippedQuantity: null,
        unscheduledNumber: null,
        commencementTime: null,
        completionTime: null,
        createBy: null,
        customerName: null,
        unifiedSocialCreditCode: null,
        customerContact: null,
        customerInformation: null,
        managementExpense: null,
        personCharge: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.number)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const number = row.number || this.ids
      getMaintenance(number).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.number != null) {
            updateMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const numbers = row.number || this.ids;
      this.$modal.confirm('是否确认删除项目维护编号为"' + numbers + '"的数据项？').then(function() {
        return delMaintenance(numbers);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/maintenance/export', {
        ...this.queryParams
      }, `maintenance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
